package zdh.avatar;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Gem Seeker
 */
public class AvatarConfig implements Config {

//    private Logger logger;
    private File file;
    private Document document;

    private String appName;
    private String appVersion;
    private String appAuthor;
    private String appRelease;
    private String elementsPath;
    private String displayWelcomePanel;

    private Node appNameNode;
    private Node appVersionNode;
    private Node appAuthorNode;
    private Node appReleaseNode;
    private Node elementsPathNode;
    private Node displayWelcomePanelNode;

    public AvatarConfig(String fileName) {
        file = new File(fileName);
//        this.logger = logger;

        // locate the XML file
        if (!file.exists()) {
            //logger.debug("config file " + fileName + " not found");
            System.err.println("Can't locate xml file.");
            return;
        }

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                .newInstance();

        DocumentBuilder documentBuilder;
        try {
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            //logger.error("parser configuration exception", e);
            return;
        }

        try {
            document = documentBuilder.parse(file);
        } catch (SAXException | IOException e) {
            //logger.error("cannot parse " + fileName, e);
            return;
        }

        Element documentElement = document.getDocumentElement();

        appNameNode = getFirstChild(documentElement, "appName");
        appVersionNode = getFirstChild(documentElement, "appVersion");
        appAuthorNode = getFirstChild(documentElement, "appAuthor");
        appReleaseNode = getFirstChild(documentElement, "appRelease");
        elementsPathNode = getFirstChild(documentElement, "elementsPath");
        displayWelcomePanelNode = getFirstChild(documentElement, "displayWelcomePanel");

    }

    private Node getFirstChild(Node parent, String childName) {
        if (parent == null || childName == null) {
            return null;
        }
        NodeList nodeList = parent.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            if (childName.equals(node.getNodeName())) {
                return node;
            }
        }
        return null;
    }

    @Override
    public void save() {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        Transformer transformer;
        try {
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            //logger.error("cannot create transformer", e);
            return;
        }

        FileWriter fileWriter;
        try {
            fileWriter = new FileWriter(file);
        } catch (IOException e) {
            //logger.error("cannot create file writer", e);
            return;
        }

        StreamResult streamResult = new StreamResult(fileWriter);
        DOMSource domSource = new DOMSource(document);
        try {
            transformer.transform(domSource, streamResult);
        } catch (TransformerException e) {
            //logger.error("cannot save config file", e);
            return;
        }
        //logger.debug("config file saved");
    }

    @Override
    public void setAppName(String appName) {
        this.appName = appName;
        appNameNode.setTextContent(appName);
    }

    @Override
    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
        appVersionNode.setTextContent(appVersion);
    }

    @Override
    public void setAuthorName(String authorName) {
        this.appAuthor = authorName;
        appAuthorNode.setTextContent(authorName);
    }

    @Override
    public void setReleaseDate(String releaseDate) {
        this.appRelease = releaseDate;
        appReleaseNode.setTextContent(releaseDate);
    }

    @Override
    public void setElementsPath(String elementsPath) {
        this.elementsPath = elementsPath;
        elementsPathNode.setTextContent(elementsPath);
    }

    @Override
    public void setDisplayWelcomePanel(boolean display) {
        if (display) {
            this.displayWelcomePanel = "true";
            displayWelcomePanelNode.setTextContent("true");
        } else {
            this.displayWelcomePanel = "false";
            displayWelcomePanelNode.setTextContent("false");
        }
    }

    @Override
    public String getAppName() {
        if (appName == null) {
            return appNameNode.getTextContent();
        }
        return null;
    }

    @Override
    public String getAppVersion() {
        if (appVersion == null) {
            return appVersionNode.getTextContent();
        }
        return null;
    }

    @Override
    public String getAuthorName() {
        if (appAuthor == null) {
            return appAuthorNode.getTextContent();
        }
        return null;
    }

    @Override
    public String getReleaseDate() {
        if (appRelease == null) {
            return appReleaseNode.getTextContent();
        }
        return null;
    }

    @Override
    public String getElementsPath() {
        if (elementsPath == null) {
            return elementsPathNode.getTextContent();
        }
        return null;
    }

    @Override
    public String getDisplayWelcomePanelOption() {
        if (displayWelcomePanel == null) {
            return displayWelcomePanelNode.getTextContent();
        }else{
            return displayWelcomePanel;
        }
    }
}
